package com.jijuxie.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jijuxie.common.core.R;
import com.jijuxie.system.domain.SysMessageTemplate;
import com.jijuxie.system.service.ISysMessageTemplateService;

@RestController
@RequestMapping("/system/message/template")
public class SysMessageTemplateController {

    @Autowired
    private ISysMessageTemplateService templateService;

    @PostMapping
    public R<Void> add(@RequestBody SysMessageTemplate template) {
        templateService.addTemplate(template);
        return R.ok();
    }

    @PutMapping
    public R<Void> update(@RequestBody SysMessageTemplate template) {
        templateService.updateTemplate(template);
        return R.ok();
    }

    @DeleteMapping("/{templateId}")
    public R<Void> delete(@PathVariable Long templateId) {
        templateService.deleteTemplate(templateId);
        return R.ok();
    }

    @PutMapping("/status")
    public R<Void> updateStatus(Long templateId, String status) {
        templateService.updateStatus(templateId, status);
        return R.ok();
    }

    @GetMapping("/list")
    public R<Page<SysMessageTemplate>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String type) {
        Page<SysMessageTemplate> page = templateService.lambdaQuery()
                .eq(type != null, SysMessageTemplate::getType, type)
                .orderByDesc(SysMessageTemplate::getCreateTime)
                .page(new Page<>(pageNum, pageSize));
        return R.ok(page);
    }
}
